﻿<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_user extends MY_Model{
    function __construct(){
        parent::__construct();
        // $this->load->library('form_validation');
        $this->load->helper('date');
    }

//==============================GET READING===========================================
    function reading($user) {
        $sql = "SELECT * FROM document as doc, reading, categories as cat 
            WHERE reading.user = '$user' 
            AND doc.category = cat.id_cat
            AND doc.id_document = reading.id_document";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

//==============================ADD BOOK FUNCTION========================================

    function add_doc($id, $name, $auth, $descript, $cate, $user, $tags, $local){

        $timeup = date('Y-m-d H:i:s',time());
        $data = array(
            'id_document' => $id,
            'name_document' => $name,
            'author' => $auth,
            'description' => $descript,
            'category' => $cate,
            'tags' => $tags,
            'location' => $local,
            'username' => $user,
            'time_upload' => $timeup
            );
        $this->db->insert('document', $data);
        return $id;
    }

//==============================ADD USER FUNCTION========================================
    function add_user($user, $pass, $fullname, $gender, $dob, $email, $phone, $ava, $job, $care){
        $data = array(
            'username' => $user,
            'password' => $pass,
            'fullname' => $fullname,
            'gender' => $gender,
            'dob' => $dob,
            'email' => $email,
            'phone' => $phone,
            'avatar' => $ava,
            'job' => $job,
            'interestedin' => $care
            );
        $query = $this->db->insert('user', $data);
        if(mysql_errno()) {
            return "MySQL error ".mysql_errno().": ".mysql_error();
        }
        return $user;
    }


//==============================CHECK LOGIN FUNCTION========================================
    function check_login($user, $pass){

        $sql = "SELECT  username, password FROM user WHERE username = '$user' AND password = '$pass'";
        $q = $this->db->query($sql);
        if($q->num_rows > 0){
            return $q->result_array();
        }
            return -1;       
    }
//==============================SHOW PROFILE FUNCTION========================================

    function show_profile($data){
         $this->db->select('*');
         $this->db->from('user');
         $this->db->where('username', $data);
         $query = $this->db->get();
         $result = $query->row_array();
         return $result;
    }
//==================================CHECK USER EXIST========================================
    function isset_user($user) {
        $this->db->where('username', $user);
        $query = $this->db->get('user');
        $result = $query->row_array();
        if($query->num_rows > 0) return true;
        return false;
    } 

//==============================SHOW PROFILE FUNCTION========================================
    function update_profile($username, $full, $gender, $dob, $phone, $job, $care){
        $data = array(
            'fullname' => $full,
            'gender' => $gender,
            'dob' => $dob,
            'phone' => $phone,
            'job' => $job,
            'interestedin' => $care
            );
        $this->db->where('username', $username);
        $this->db->update('user', $data);
    }
//===============================GET USER FUNCTION===============================================
    function get_user($username) {
        $this->db->where('username', $username);
        $query = $this->db->get('user');

        $user = $query->row_array();
        return $user;
    }

//==============================GET LOCATION FUNCTION========================================
    public function get_loca() {
        $query = $this->db->query("SELECT id_location, name_location, type_location FROM location WHERE parent_location = 'null'");
        $list = [];
        foreach ($query->result() as $row) {
            array_push($list, $row);
        }
        return $list;
    }

    public function get_loca_name($idloca) {
        $where = $this->db->where("id_location", $idloca);
        $q = $this->db->get("location");
        $loca = $q->row_array();
        return $loca['name_location'];
    }

//===============================REMOVE READING=============================================
    public function remove_reading($bookId, $user) {
        $this->db->delete('reading', array('user'=>$user, 'id_document'=>$bookId));
    }

//===============================ADD TAGS=============================================
    public function add_tags($bookId, $content, $location){
        $id = 'tag'.time();
        $data = array(
            'id_tags' => $id,
            'name_tags' => $content,
            'id_doc' => $bookId,
            'id_chap' => '',
            'location' => $location
        );
        $this->db->insert('tags', $data);
        return $id;
    }
}
?>